package com.lenovo.smarttraffic.ui.activity.stage2;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarInfo;
import com.lenovo.smarttraffic.bean.UserBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.ui.adapter.AccountChargeAdapter;
import com.lenovo.smarttraffic.util.HttpUtils;

public class AccountChargeActivity extends BaseActivity {

    private ListView listView;
    private AccountChargeAdapter adapter;
    private CarInfo account;
    private UserBean user;

    private TextView charge_history;


    @Override
    protected int getLayout() {
        return R.layout.activity_account_manage;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }

    }

    public void initView() {
        Toolbar mToolbar = findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(view -> finish());

        listView = findViewById(R.id.listview_account_manage);

        charge_history = findViewById(R.id.charge_history);
        charge_history.setOnClickListener(v -> startActivity(new Intent().setClass(AccountChargeActivity.this, ChargeHistoryActivity.class)));

    }

    private void initData() {
        HttpUtils.post(this, "get_car_info",
                new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> HttpUtils.post(this, "get_all_user_info",
                        new HttpUtils().setParam("UserName", MainActivity.userName),
                        response2 -> {
                            account = new Gson().fromJson(response.toString(), CarInfo.class);
                            user = new Gson().fromJson(response2.toString(), UserBean.class);
                            adapter = new AccountChargeAdapter(this, account, user);
                            listView.setAdapter(adapter);
                        }));
    }

}
